//
//  HEHealthPlaneController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/15.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEHealthPlaneController.h"
#import "HEHealthPlaneModel.h"
#import "HEHealthPlaneCell.h"
#import "HEHealthPlaneDetailsController.h"

@interface HEHealthPlaneController ()<UITableViewDelegate, UITableViewDataSource>


@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;
 
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HEHealthPlaneModel *> *dataSourceArr;

@end

@implementation HEHealthPlaneController

- (NSMutableArray <HEHealthPlaneModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"健康规划";
    _pageNum = 1;
    _pageSize = 20;

    [self initUI];

    [self loadData];
    [self setCustomTableView:_tableView];
}


- (void)initUI {
 
    
    _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight ) style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.separatorColor = KHexColor(@"EEEEEE");

    _tableView.scrollsToTop = YES;
    _tableView.tableFooterView = [[UIView alloc] init];

    self.tableView.backgroundColor = CViewBgColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

    __weak __typeof(&*self)weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        weakSelf.isFromHeadRefresh = YES;
        weakSelf.pageNum = 1;
        [weakSelf loadData];
    }];

    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        weakSelf.isFromHeadRefresh = NO;
        weakSelf.pageNum += 1;
        [weakSelf loadData];
    }];

    [_tableView registerClass:[HEHealthPlaneCell class] forCellReuseIdentifier: KHEHealthPlaneCell];
}


#pragma mark-

- (void)loadData {
    
    HEPageRequest *params = [HEPageRequest new];
    params.pageNum = _pageNum;
    params.pageSize = _pageSize;
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KUserHis parameters:params success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }

        HEPageResponse *response = [HEPageResponse mj_objectWithKeyValues:responseObject];
        [self.dataSourceArr addObjectsFromArray:[HEHealthPlaneModel mj_objectArrayWithKeyValuesArray:response.data.list]];
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.list.count == 0) {
                [Dialog toast:@"没有更多数据了~" delay:2.0];
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEHealthPlaneCell *cell = [tableView dequeueReusableCellWithIdentifier: KHEHealthPlaneCell];
    [cell configCellWithModel: _dataSourceArr[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: KHEHealthPlaneCell cacheByIndexPath:indexPath configuration:^(HEHealthPlaneCell *cell) {
        [cell configCellWithModel: self.dataSourceArr[indexPath.row]];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEHealthPlaneModel *model = _dataSourceArr[indexPath.row];
    
    HEHealthPlaneDetailsController *vc = [HEHealthPlaneDetailsController new];
    vc.inst_id = model.inst_id;
    [self.navigationController pushViewController:vc animated:YES];
}
 
@end
